package com.example.tool.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.listener.OnItemListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created Date 2019/6/10
 *
 * @author liangkun
 * description：盒端-- item适配器
 */
public class HomeRvAdapter extends RecyclerView.Adapter<HomeRvAdapter.HomeHolder> {
    private List<String> fileList;

    public HomeRvAdapter(List<String> fileList) {
        this.fileList = fileList;
    }

    @NonNull
    @Override
    public HomeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.home_list, parent, false);
        return new HomeHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull HomeHolder holder, final int position) {
        if (fileList != null && fileList.size() > 0) {
            holder.tvItem.setText(fileList.get(position));
        }
        holder.cardItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemListener != null) {
                    onItemListener.OnItemClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return fileList == null ? 0 : fileList.size();
    }


    class HomeHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.card_home_item)
        CardView cardItem;
        @BindView(R.id.tv_home_item)
        TextView tvItem;

        private HomeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public OnItemListener onItemListener;

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;

    }
}
